var y,y1,y2,y3;
var app = getApp();
var util = require('../../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isaddedit:false,  //添加编辑分类弹窗
    isdel:false,    //删除弹窗
    itemshow:[],    //二级分类显示
    isdelmodel: false,  //删除模板弹窗
    time: 30,       //验证码倒计时
    numshow: true,  //显示'获取验证码'
    classlist:[],
    move_index:-1,   //拖拽索引
    move_y: '',
    ispaixu:false,  //是否编辑排序
  },
  // 添加-编辑分类弹窗
  window:function(){
    this.setData({
      isaddedit: true,
      shopname:''
    })
  },
  // 添加一级分类
  oneadd:function(){
    this.window();
    this.setData({
      class_belong: 0,
      private_class_belong:0,
      shopname: "",
      title:"添加产品分类"
    })   
  },
  // 添加二级分类
  twoadd: function (e) {
    this.window();
    this.setData({
      class_belong: 0,
      private_class_belong: e.target.id,
      show_index:e.currentTarget.dataset.index,
      shopname:"",
      title: "添加产品分类"
    })
  },
  // 编辑一级分类
  oneedit: function (e) {
    this.window();
    this.setData({
      class_belong: 1,
      private_class_belong: e.target.id,
      shopname: e.target.dataset.name,
      title: "编辑产品分类"
    })
  },
  // 编辑二级分类
  twoedit: function (e) {
    this.window();
    this.setData({
      class_belong: 1,
      private_class_belong: e.target.id,
      shopname: e.target.dataset.name,
      title: "编辑产品分类"
    })
  },
  // 产品名称输入
  nameinput: function (e) {
    this.setData({
      shopname: e.detail.value
    })
  },
  // 关闭添加-编辑分类弹窗
  atcancel: function () {
    this.setData({
      isaddedit: false
    })
  },
  // 确定并关闭添加-编辑
  atconfirm: function () {
    var that = this;
    util.buttonClicked(this);  //防止重复点击
    app.AppHttp('api/Supplier/AjaxAddPrivateCate', {
      id: app.globalData.loginInfo.id,
      class_belong: that.data.class_belong,
      private_class_name: that.data.shopname,
      private_class_belong: that.data.private_class_belong,
      e_type:'client'
    }, function (res) {
      app.globalData.shop_manage = 1;
      wx.showToast({
        title: res.data.msg
      });
      if(that.data.private_class_belong>0){
        that.setData({
          ["itemshow[" + that.data.show_index + "]"]: true
        });
      }else{
        that.setData({
          itemshow:[]
        });
      };
      if (res.data.status != -1){
        that.setData({
          isaddedit: false
        })
        that.upshoplist();
      };    
    })
  },
  // 删除分类弹窗
  del:function(e){
    this.setData({
      isdel:true,
      shopid:e.target.id,
      del_level:e.currentTarget.dataset.level
    })
  },
  // 关闭删除分类弹窗
  delcancel:function(){
    this.setData({
      isdel: false
    })
  },
  // 确认删除分类
  delconfirm:function(){
    var that = this;
    app.AppHttp('api/Supplier/AjaxDelClass', {
      supplier_id: app.globalData.loginInfo.id,
      id: that.data.shopid
    }, function (res) {
      // console.log(JSON.stringify(res.data));
      app.globalData.shop_manage = 1;
      wx.showToast({
        title: res.data.msg
      },2000)
      that.setData({
        isdel: false
      });
      if(that.data.del_level==1){
        that.setData({
          isdel: false,
          itemshow:[]
        });
      };
      that.upshoplist();
    })   
  },
  // 显示二级分类
  showck:function(e){
    var index = e.currentTarget.dataset.id
    this.setData({
      ["itemshow[" + index + "]"]: true
    })
  },
  // 关闭二级分类
  hideck:function (e) {
    var index = e.currentTarget.dataset.id;
    this.setData({
      ["itemshow[" + index + "]"]: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      template_id: options.template_id,
      item_h: 90 / 750 * wx.getSystemInfoSync().windowWidth
    });
  },
  upshoplist: function () {   
    wx.showLoading({
      title: '加载中...',
    }) 
    var that = this;
    app.AppHttp('api/Supplier/getPrivateCate', {
      id: app.globalData.loginInfo.id,
      e_type:'client'
    }, function (res) {
      console.log(JSON.stringify(res.data))
      that.setData({
        classlist: res.data[0]
      })
      if(res.data.status==-1){
        that.setData({
          classlist: [],
          template_id: undefined
        })
      }
      wx.hideLoading();
    })
  },
  // 删除模板弹窗
  delmodel: function () {
    this.setData({
      isdelmodel: true,
      code:''
    })
  },
  // 关闭删除模板弹窗
  delmodelcancel: function () {
    this.setData({
      isdelmodel: false
    })
  },
  // 输入验证码
  codeinput: function (e) {
    this.setData({
      code: e.detail.value
    });
  },
  // 获取验证码
  getcode: function (event) {
    var that = this;
    var times = that.times
    app.AppHttp('api/Supplier/AjaxVerificationCode', {
      phone: that.data.phone
    }, function (res) {
      console.log(JSON.stringify(res.data))
    })
    that.setData({
      numshow: false
    })
    that.data.setInter = setInterval(times, 1000);
  },
  // 倒计时
  times: function () {
    var time = this.data.time;
    time--;
    if (time == 0) {
      this.setData({
        numshow: true,
        time: 10
      });
      clearInterval(this.data.setInter);
      return;
    }
    this.setData({
      time: time
    });
  },
  // 确定并关闭删除模板弹窗
  delmodelconfirm: function () {
    var that = this;
    console.log(that.data.code);
    if (that.data.code==''){
      wx.showToast({
        title:'请输入验证码'
      })
    }else{
      app.AppHttp('api/Supplier/deleteTemplate', {
        id: that.data.template_id,
        supplier_id: app.globalData.loginInfo.id,
        phone: that.data.phone,
        code: that.data.code,
        e_type:'client'
      }, function (res) {
        console.log(JSON.stringify(res.data));
        app.globalData.shop_manage = 1;
        wx.showToast({
          title: res.data.msg
        })
        that.setData({
          isdelmodel: false
        });
        if(res.data.status==1){
          wx.switchTab({
            url:'/pages/shangpin/shangpin'
          });
        }      
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.upshoplist();
    this.setData({
      phone: app.globalData.loginInfo.phone,
      tel: app.globalData.loginInfo.phone.substr(0, 3) + '****' + app.globalData.loginInfo.phone.substr(7)
    })
  },
  // 编辑排序
  paixu_ck: function () {
    this.setData({
      ispaixu: true
    });
  },
  // 完成排序
  conf_paixu_ck:function(){
    var that = this;
    that.setData({
      ispaixu:false
    });
  
  },
  // 开始拖拽
  movestart:function(e){ 
    var index = e.currentTarget.dataset.index;
    y = e.touches[0].clientY;         //开始触摸坐标
    y1 = e.currentTarget.dataset.item_h;   //元素初始距顶部的距离  
    this.setData({
      move_index: e.currentTarget.dataset.index,      //触摸移动元素的索引
      move_index_zero: e.currentTarget.dataset.index,   //开始触摸时的初始索引
      move_y:y1    //初始顶部位置
    });
  },
  // 拖拽中
  move:function(e){   
    y2 = e.touches[0].clientY - y;      //触摸中移动距离
    y3 = y2+y1;      //移动中元素距顶部的距离
    if(y3>-this.data.item_h/2){
      var underIndex = this.getCurrnetUnderIndex(); 
    };
    var move_index = this.data.move_index;
    if (underIndex != null && underIndex != this.data.move_index) {
      var arr = this.data.classlist;
      this.changeArrayData(arr, underIndex, this.data.move_index);
      this.setData({
        classlist: arr,
        move_index: underIndex
      })
    }
    this.setData({
      move_y: y3
    })
  },
  // 结束拖拽
  moveend:function(e){
    var sort_arr = []; //排序id数组
    for(var i=0;i<this.data.classlist.length;i++){
      sort_arr[i] = this.data.classlist[i].id;
    };
    app.AppHttp('api/Supplier/changeCateOrder', {
      cate_array: sort_arr
    }, function (res) {
      app.globalData.shop_manage = 1;
      console.log(JSON.stringify(res.data));
    });
    this.setData({
      move_index: -1
    });
  },
  // 改变数组顺序
  changeArrayData: function (arr, i1, i2){
    var temp = arr[i1];
    arr[i1] = arr[i2];
    arr[i2] = temp;
  },
  // 计算移动元素下方的索引
  getCurrnetUnderIndex:function(){
    if (y2>=0){
      var endy = y2 + this.data.item_h/2;
    }else{
      var endy = y2 - this.data.item_h/2;
    }   
    var addindex = parseInt(endy/this.data.item_h); 
    var underIndex = this.data.move_index_zero + addindex;
    if (underIndex > this.data.classlist.length - 1){
      return null;
    }else{
      return underIndex;
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})